package middleware

import (
	"fmt"
	"gin-c/frame"
	"path"
	"strings"

	"github.com/gin-gonic/gin"
)

// 判断是否登录
func IsLogin(filter []string) gin.HandlerFunc {
	return func(c *gin.Context) {

		reqPath := path.Base(c.Request.URL.Path)

		if !frame.Contains(filter, reqPath) {

			token := strings.TrimPrefix(c.GetHeader("Authorization"), "Bearer ")
			if token == "" {
				panic([]any{7501, "token is empty"})
			}
			data, err := frame.TokenVerify(token)
			if err != nil {
				panic([]any{7501, err.Error()})
			}
			tokenData, err := frame.MapToJSON(map[string]any(data))
			if err != nil {
				panic([]any{7501, err.Error()})
			}
			c.Set("_data", tokenData)

		}
		c.Next()
	}
}

func Test(c *gin.Context) {
	fmt.Printf("中间件被执行了\n")
	c.Next()
}
